# -*- coding: utf-8 -*-
# @Time    : 2018-05-29 16:07
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : Rule20_HighRisk_extention.py
# @Software: PyCharm


'''eof
name:高风险展期
code:HighRisk_extention
tableName:
columnName:
groups:准入>个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json
import datetime
import re
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', None, 'null', '/', 'Null']

def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def HighRiskExtention():
    try:
        returnList = []
        # 贷款特殊交易
        icrSpecialTradeLoanList = FormatData("ICRSpecialTrade*Loan")
        if icrSpecialTradeLoanList == [{}]:
            return u"缺失值"
        # 准贷记卡特殊交易
        for icrSpecialTradeLoan in icrSpecialTradeLoanList:
            try:
                tradeType = icrSpecialTradeLoan.get("TradeType")
                if tradeType in null_type_list:
                    returnList.append(None)
                    continue
                if u"展期" in tradeType:
                    returnList.append(1)
                else:
                    returnList.append(0)
            except:
                returnList.append(None)
                continue
        returnListNotNone = filter(lambda x: x is not None, returnList)
        if len(returnListNotNone) == 0:
            return u'缺失值'
        elif 1 in returnListNotNone:
            return 1
        else:
            return 0
    except:
        return u"缺失值"

result = HighRiskExtention()